//door.txt - A door. This is the default script for all doors. If left alone,
// door starts out closed and unlocked.

//Cell 0 - The lock difficulty. If 0, unlocked. Otherwise, the level of Tool Use
//  necessary to open it. (Goes by the maximum Tool Use in party, not the sum).
//Cell 1 - The required key. If 0, none. Otherwise, the number of the special item required to open the door.
//Cell 2,3 - The sdf which is set to 1 when door is unlocked.
//Cell 4 - If 1, start door as open
//Cell 5 - level of magical protection (start at about 6)

beginobjectscript; // door lever

variables;
	short door_is_open = 0;
	short i_am_unlocked = 0;
	short block_open = 0;
	short start_open = 0;
	short give_xp = 0;
	short magic_block = 0;
	
body;


beginstate INIT_STATE;
	if (get_memory_cell(5) > 0) 
		magic_block = 1;
	if (get_memory_cell(0) == 0)
		i_am_unlocked = 1;
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0) {
			i_am_unlocked = 1;
			magic_block = 0;
			}
		}
	if (get_memory_cell(4) > 0) {
		start_open = 1;
		//give_object_message(ME,103);
		set_object_blockage(ME,0);
		if ((object_type(ME) == 120) || (object_type(ME) == 124))
			set_object_icon(ME,3);
		if ((object_type(ME) == 121) || (object_type(ME) == 125))
			set_object_icon(ME,2);
		}
	break;

beginstate DEAD_STATE;

break;

beginstate START_STATE; // door closed, waiting
	if ((door_is_open == 0) && ((my_current_message() == 103) || (start_open > 0))) {
		start_open = 0;
		i_am_unlocked = 1;
		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
			set_flag(get_memory_cell(2),get_memory_cell(3),1);
		
		door_is_open = 1;
		set_object_blockage(ME,0);
		if ((object_type(ME) == 120) || (object_type(ME) == 124))
			set_object_icon(ME,3);
		if ((object_type(ME) == 121) || (object_type(ME) == 125))
			set_object_icon(ME,2);
		}
	if ((door_is_open == 1) && (my_current_message() == 104)) { // close door mess
		if (creature_on_obj(ME) >= 0)
			end();
		door_is_open = 0;
		set_object_blockage(ME,3);
		if ((object_type(ME) == 120) || (object_type(ME) == 124))
			set_object_icon(ME,1);
		if ((object_type(ME) == 121) || (object_type(ME) == 125))
			set_object_icon(ME,0);
		}
break;

beginstate USE_STATE;
	block_open = 0;
		
	if (door_is_open > 0) {
		if (creature_on_obj(ME) >= 0) {
			print_str("Close Door: A character is blocking the door.");
			end();
			}
		if (get_nearest_evil_char(6) >= 0) {
			print_str("Close Door: A nearby enemy keeps you from closing the door.");
			end();
			}
		print_str_color("Close Door: You close the door.",2);
		door_is_open = 0;
		set_object_blockage(ME,3);
		if ((object_type(ME) == 120) || (object_type(ME) == 124))
			set_object_icon(ME,1);
		if ((object_type(ME) == 121) || (object_type(ME) == 125))
			set_object_icon(ME,0);

		pc_heard_sound(32);
		end();	
		}

	
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
			i_am_unlocked = 1;
		}

	if ((get_memory_cell(1) > 0) && (i_am_unlocked == 0)) {
		if (has_spec_item(get_memory_cell(1)) > 0) {
			print_str_color("Open Door: You use a key to unlock the door.",2);
			i_am_unlocked = 1;
			}

		}
		
	if ((magic_block > 0) && (i_am_unlocked == 0)) {
		if (get_memory_cell(1) > 0) {
			print_str_color("Open Door: The door is held shut with magic bonds. Unless",2);
			print_str_color("  you can find the key, you will need to break them with a spell.",2);
			}
			else {
				print_str_color("Open Door: The door is held shut with magic bonds. You will",2);
				print_str_color("  need to break them with a spell.",2);
				}
		end();
		}
		
	if ((block_open == 0) && (get_memory_cell(0) > 0) && (i_am_unlocked == 0)) {
		block_open = 1;
		if (get_stat_total(24) >= get_memory_cell(0)) {
			print_str_color("Open Door: The door is locked, but you manage to pick the lock.",2);
			block_open = 0;
			i_am_unlocked = 1;
			give_xp = 1;
			}
			else { 
				if (get_memory_cell(0) >= 90) {
					print_str_color("Open Door: The door is locked. You don't have the key, and",2);
					print_str_color("  the lock is too complicated to be picked.",2);
					}
					else if (get_memory_cell(1) > 0) {
						print_str("Open Door: The door is locked. You don't have the key, and your Tool Use skill");
						print_big_str("  isn't high enough to pick it. (Difficulty: ",get_memory_cell(0),")");
						}
					else {
						print_str("Open Door: The door is locked, and your Tool Use skill isn't high");
						print_big_str("  enough to pick it. (Difficulty: ",get_memory_cell(0),")");
						}

				end();
				}
		}
		
	if (block_open == 0) {
		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
			if ((give_xp > 0) && (get_flag(get_memory_cell(2),get_memory_cell(3)) == 0))
				award_party_xp(BASE_TRAP_XP,get_memory_cell(0));

			if (gf(get_memory_cell(2),get_memory_cell(3)) == 0)
				set_flag(get_memory_cell(2),get_memory_cell(3),1);
			}
		print_str_color("Open Door: You open the door.",2);
		pc_heard_sound(18);
		door_is_open = 1;
		set_object_blockage(ME,0);
		if ((object_type(ME) == 120) || (object_type(ME) == 124))
			set_object_icon(ME,3);
		if ((object_type(ME) == 121) || (object_type(ME) == 125))
			set_object_icon(ME,2);
		}
break;

beginstate UNLOCK_STATE;
	// unlock spell only affects one half of doors
	if (door_is_open > 0) {
		print_str_color("Unlock Spell: The door is already open.",2);
		end();
		}
	if (i_am_unlocked > 0) {
		print_str_color("Unlock Spell: The door is already unlocked.",2);
		end();
		}


	if (current_unlock_strength() >= get_memory_cell(0)) {
		print_str_color("Unlock Spell: The spell breaks the lock.",2);
		magic_block = 0;
		i_am_unlocked = 1;
		play_sound(18);
		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
			set_flag(get_memory_cell(2),get_memory_cell(3),1);
		end();
		}
	print_str_color("Unlock Spell: The spell is too weak to affect the lock.",2);	
break;
